/*
 * GamePanel.java
 *
 * Created on 12. juillet 2007, 13:51
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package pacman;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.*;

/**
 * The JPanel used to draw the game
 */
public class GamePanel extends JPanel {
    
    private Graphics DrawBuffer;
    private Image DrawImage;
    private Map imgMap=new TreeMap();
    private cCore parent;
    
    /**
     * Creates a new instance of GamePanel
     * @param parent The cCore game manager of the game
     */
    public GamePanel(cCore parent) {
        this.parent=parent;
    }
    
    /**
     * Generate a capture of the game in order to draw it on the panel
     */
    public void GenImage(){ 
        Image img;
                
        Graphics g=this.getGraphics();
        
        if(DrawBuffer==null || this.getWidth()!=DrawImage.getWidth(this) || this.getHeight()!=DrawImage.getHeight(this)){
            DrawImage=createImage(this.getWidth(),this.getHeight());
            DrawBuffer=DrawImage.getGraphics();
        }
               
        
        iCase[][] plateau=parent.getPlateau();
        
        DrawBuffer.clearRect(0,0,DrawImage.getWidth(this),DrawImage.getHeight(this));
        
        int CaseX=this.getWidth()/plateau.length;
        int CaseY=this.getHeight()/plateau[0].length;        
        
        for(int i=0;i<plateau.length;i++){
            for(int j=0;j<plateau[0].length;j++){
                DrawBuffer.drawImage(getImage(plateau[i][j].getImageName()),i*CaseX,j*CaseY,CaseX,CaseY,this);
            }
        }
        
        for(int i=0;i<parent.getFoodList().size();i++){
            Position pos=parent.getFoodList().get(i).getPosition();
            DrawBuffer.drawImage(getImage(parent.getFoodList().get(i).getImageName()),pos.getX()*CaseX,pos.getY()*CaseY,CaseX,CaseY,this);
        }
                               
        for(int i=0;i<parent.getGhostList().size();i++){
            DrawBuffer.drawImage(getImage(parent.getGhostList().get(i).getImageName()),parent.getGhostList().get(i).getPosition().getX()*CaseX,parent.getGhostList().get(i).getPosition().getY()*CaseY,CaseX,CaseY,this);
        }
        
        for(int i=0;i<parent.getPacmanList().size();i++){
            cPacman pacman=parent.getPacmanList().get(i);
            DrawBuffer.drawImage(getImage(pacman.getImageName()),pacman.getPosition().getX()*CaseX,pacman.getPosition().getY()*CaseY,CaseX,CaseY,this);
        }
        
        
        img=getImage("win");
        if(parent.isWinned()){
            DrawBuffer.drawImage(getImage("win"),(DrawImage.getWidth(this)-DrawImage.getWidth(this)/5)/2,(DrawImage.getHeight(this)-DrawImage.getWidth(this)/10)/2,DrawImage.getWidth(this)/5,DrawImage.getWidth(this)/10,this);
        }
        
        img=getImage("game_over");
        if(parent.isGameOvered()){
            DrawBuffer.drawImage(getImage("game_over"),(DrawImage.getWidth(this)-img.getWidth(this))/2,(DrawImage.getHeight(this)-img.getHeight(this))/2,img.getWidth(this),img.getHeight(this),this);
        } 
        
        img=getImage("pause");
        if(parent.isPaused()){
            DrawBuffer.drawImage(getImage("pause"),(DrawImage.getWidth(this)-DrawImage.getWidth(this)/5)/2,(DrawImage.getHeight(this)-DrawImage.getWidth(this)/10)/2,DrawImage.getWidth(this)/5,DrawImage.getWidth(this)/10,this);
        }
    }
    
    private Image getImage(String key){
        if(imgMap.get(key)!=null){
            return (Image)imgMap.get(key);
        }
        return getImage("blank");
    }
    
    /**
     * Loads all images of the theme
     * @param theme Theme to load
     */
    public void LoadImages(String theme){
        Graphics g=this.getGraphics();
        File dir=new File("themes/"+theme);
        for(int i=0;i<dir.list().length;i++){
            if(dir.list()[i].substring(dir.list()[i].length()-3,dir.list()[i].length()).equalsIgnoreCase("png")){
                imgMap.put(dir.list()[i].substring(0,dir.list()[i].length()-4),Toolkit.getDefaultToolkit().getImage(dir.listFiles()[i].getAbsolutePath()));
                g.drawImage((Image)imgMap.get(dir.list()[i].substring(0,dir.list()[i].length()-4)),0,0,100,100,this);
            }
        }
    }
    
    /**
     * Override of the panel's paint method
     * @param g The graphics of the panel
     */
    public void paint(Graphics g){
        g.drawImage(DrawImage,0,0,this.getWidth(),this.getHeight(),this);
    }
    
}
